package com.fengye.rabbit.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengye.rabbit.order.domain.MqOrder;
import com.fengye.rabbit.order.domain.UserOrder;
import com.fengye.rabbit.order.dto.MessageDTO;
import com.fengye.rabbit.order.dto.UserOrderDTO;
import com.fengye.rabbit.order.exception.ServiceException;
import com.fengye.rabbit.order.mapper.UserOrderMapper;
import com.fengye.rabbit.order.rabbit.OrderPublisher;
import com.fengye.rabbit.order.service.MqOrderService;
import com.fengye.rabbit.order.service.UserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 *
 */
@Service
@Slf4j
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder>
        implements UserOrderService {

    @Resource
    private UserOrderMapper userOrderMapper;

    @Resource
    private MqOrderService mqOrderService;

    @Autowired
    private OrderPublisher orderPublisher;

    /**
     * 用户下单
     * @param dto {@link UserOrderDTO} 下单对象
     */
    @Override
    public void pushUserOrder(UserOrderDTO dto) {
        try {
            UserOrder userOrder = new UserOrder();
            BeanUtils.copyProperties(dto, userOrder);
            log.info("dto={},userOrder={}", dto, userOrder);

            // 已保存
            userOrder.setStatus(1);

            userOrder.setCreateTime(new Date());

            // userOrderMapper.insert(userOrder);
            this.save(userOrder);
            log.info("用户已下单，下单信息为：{}", userOrder);

            log.info("将下单信息为：{} 压入死信队列", userOrder);
            orderPublisher.sendMsg(new MessageDTO(userOrder.getOrderNo()));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 更新下单记录的状态
     * @param userOrder
     */
    public void updateUserOrderRecord(UserOrder userOrder) {
        try {
            if (userOrder != null) {
                // 失效该订单
                userOrder.setIsActive(0);

                // 设置更新时间
                userOrder.setUpdateTime(new Date());

                // userOrderMapper.updateById(userOrder);
                this.updateById(userOrder);
                log.info("更新订单完成");

                // 开始记录到 mq_order
                MqOrder mqOrder = new MqOrder();
                mqOrder.setOrderId(userOrder.getUserId().toString());
                mqOrder.setBusinessTime(new Date());
                mqOrder.setMemo("更新当前用户下单记录 Id，orderId=" + userOrder.getOrderNo());
                mqOrderService.save(mqOrder);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }
}




